﻿Imports System
Imports System.Management
Imports System.Runtime.InteropServices
Imports System.Web.Security



Public Class CheckWMI
    Inherits System.Web.UI.Page
    Public Shared connection As ConnectionOptions
    Public Shared Query As ObjectQuery
    Public Shared pcname As String


#Region "External"
    <DllImport("advapi32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function LogonUser(userName As String, domain As String, password As String, logonType As Integer, logonProvider As Integer, ByRef accessToken As IntPtr) As Boolean
    End Function

    Private Declare Unicode Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerW" (lpMachineName As String, lpDatabaseName As String, dwDesiredAccess As SC_MANAGER_ACCESS) As IntPtr

    <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function CreateService(hSCManager As IntPtr, lpServiceName As String, lpDisplayName As String, dwDesiredAccess As SC_MANAGER_ACCESS, dwServiceType As SC_SERVICE_TYPE, dwStartType As SC_START_TYPE, _
            dwErrorControl As SC_ERROR_CONTROL, lpBinaryPathName As String, lpLoadOrderGroup As String, lpdwTagId As IntPtr, lpDependencies As String, lpServiceStartName As String, _
            lpPassword As String) As IntPtr
    End Function

    <DllImport("advapi32", SetLastError:=True)> _
    Friend Shared Function StartService(hService As IntPtr, dwNumServiceArgs As Integer, lpServiceArgVectors As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("advapi32.dll", SetLastError:=True)> _
    Friend Shared Function DeleteService(hService As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Friend Shared Function OpenService(hSCManager As IntPtr, lpServiceName As String, dwDesiredAccess As SC_MANAGER_ACCESS) As IntPtr
    End Function

    <DllImport("mpr.dll")> _
    Private Shared Function WNetAddConnection3(hWndOwner As IntPtr, ByRef lpNetResource As NETRESOURCE, lpPassword As String, lpUserName As String, dwFlags As Integer) As Integer
    End Function

    <DllImport("advapi32.dll", SetLastError:=True)> _
    Private Shared Function CloseServiceHandle(hSCObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

#End Region

#Region "Flags"

    <Flags> _
    Friend Enum SC_MANAGER_ACCESS As UInteger
        CONNECT = &H1
        CREATE_SERVICE = &H2
        ENUMERATE_SERVICE = &H4
        LOCK = &H8
        QUERY_LOCK_STATUS = &H10
        MODIFY_BOOT_CONFIG = &H20

        STANDARD_RIGHTS_REQUIRED = &HF0000

        GENERIC_READ = &H80000000UI
        GENERIC_WRITE = &H40000000
        GENERIC_EXECUTE = &H20000000
        GENERIC_ALL = &H10000000

        ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or CONNECT Or CREATE_SERVICE Or ENUMERATE_SERVICE Or LOCK Or QUERY_LOCK_STATUS Or MODIFY_BOOT_CONFIG
    End Enum

    Friend Enum SC_SERVICE_TYPE As UInteger
        KERNEL_DRIVER = &H1
        FILE_SYSTEM_DRIVER = &H2
        ADAPTER = &H4
        RECOGNIZER_DRIVER = &H8

        DRIVER = (KERNEL_DRIVER Or FILE_SYSTEM_DRIVER Or RECOGNIZER_DRIVER)

        WIN32_OWN_PROCESS = &H10
        WIN32_SHARE_PROCESS = &H20
        WIN32 = (WIN32_OWN_PROCESS Or WIN32_SHARE_PROCESS)

        INTERACTIVE_PROCESS = &H100

        TYPE_ALL = (WIN32 Or ADAPTER Or DRIVER Or INTERACTIVE_PROCESS)
    End Enum

    Friend Enum SC_START_TYPE As Integer
        SERVICE_BOOT_START = &H0
        SERVICE_SYSTEM_START = &H1
        SERVICE_AUTO_START = &H2
        SERVICE_DEMAND_START = &H3
        SERVICE_DISABLED = &H4
    End Enum

    Friend Enum SC_ERROR_CONTROL As Integer
        SERVICE_ERROR_IGNORE = &H0
        SERVICE_ERROR_NORMAL = &H1
        SERVICE_ERROR_SEVERE = &H2
        SERVICE_ERROR_CRITICAL = &H3
    End Enum

    Private Structure NETRESOURCE
        Friend dwScope As Integer
        Friend dwType As Integer
        Friend dwDisplayType As Integer
        Friend dwUsage As Integer
        Friend LocalName As String
        Friend RemoteName As String
        Friend Comment As String
        Friend Provider As String
    End Structure

    Friend Const RESOURCETYPE_ANY As Integer = &H0
    Friend Const CONNECT_INTERACTIVE As Integer = &H8
    Friend Const CONNECT_PROMPT As Integer = &H10

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'TextBox2.Text = 
        'System.Net.CredentialCache.DefaultCredentials
        connection = New ConnectionOptions
        'connection.Authentication = AuthenticationLevel.PacketPrivacy
        connection.EnablePrivileges = True
        'connection.Impersonation = ImpersonationLevel.Delegate
        connection.Impersonation = ImpersonationLevel.Impersonate
        '.Authentication = AuthenticationLevel.Default
        'connection.EnablePrivileges = True

        'connection.Username = "AAM"
        'connection.Password = "Hertam]]\\"
        'connection.Authority = "ntlmdomain:SBCORP.UB.SBRF.RU"
    End Sub

    Protected Sub ButtonCheck_Click(sender As Object, e As EventArgs) Handles ButtonCheck.Click
        Try
            Dim pngstatus As Integer = PingingPC(TextBoxEnterComp.Text)
            If pngstatus = 0 Then
                LabelStatus.BackColor = System.Drawing.ColorTranslator.FromHtml("#EFEEEF") 'FF,34,00}
                LabelStatus.Text = ""
                Dim IP = "", MAC = "", OS = "", OSSP = ""
                pcname = TextBoxEnterComp.Text
                TextBoxCompName.Text = GetNicConfig("DNSHostName")

                Button1.Text = System.Security.Principal.WindowsIdentity.GetCurrent.Name & " = " & Page.User.Identity.Name & " = " & System.Threading.Thread.CurrentPrincipal.Identity.Name
                ' Get IP, MAC

                TextBoxIP.Text = GetNicConfig("IP")
                TextBoxIPSubnet.Text = GetNicConfig("Gateway")
                TextBoxMAC.Text = GetNicConfig("MAC")

                ' Get OS

                TextBoxOS.Text = GetOS("OS") & " (" & GetOS("ARCH") & ")"
                TextBoxOSPack.Text = GetOS("SP")

                ' Get SiteCode, Client Version

                TextBoxSiteCode.Text = GetClientInfo("SiteCode")
                TextBoxVersion.Text = GetClientInfo("ClientVer")

                ' Get MP
                TextBoxMP.Text = GetMP()

                ' Get GUID
                TextBoxGUID.Text = GetGUID("ClientId")
                Dim date_tmp As String = GetGUID("ClientIdChangeDate")
                date_tmp = DateStringToDate(date_tmp)
                TextBoxGUID_ChangeDate.Text = date_tmp

                ' Get HB, HW, SW Inventory last date
                TextBoxHBDisc.Text = WMIDateStringToDate(GetInventoryDate("HB"))
                TextBoxHWInv.Text = WMIDateStringToDate(GetInventoryDate("HW"))
                TextBoxSWInv.Text = WMIDateStringToDate(GetInventoryDate("SW"))

            Else
                LabelStatus.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF3400") 'FF,34,00}
                LabelStatus.Text = "ПК недоступен. PingStatus = " & pngstatus
            End If

        Catch err As ManagementException
            LabelStatus.Text = err.Message
        Catch unauthorizeErr As System.UnauthorizedAccessException
            LabelStatus.Text = unauthorizeErr.Message
        End Try



    End Sub


    ' ===========================================================================================

    ' ===========================================================================================
    Function GetInventoryDate(ByVal x As String)
        Try

            Dim GetInventoryDate_tmp As String = ""
            Dim path As New ManagementPath("\\" & pcname & "\root\ccm\invagt")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()
            Dim strQuery As New ObjectQuery
            If x = "HB" Then
                strQuery = New ObjectQuery("SELECT * FROM InventoryActionStatus WHERE InventoryActionID='{00000000-0000-0000-0000-000000000003}'")
            ElseIf x = "HW" Then
                strQuery = New ObjectQuery("SELECT * FROM InventoryActionStatus WHERE InventoryActionID='{00000000-0000-0000-0000-000000000001}'")
            ElseIf x = "SW" Then
                strQuery = New ObjectQuery("SELECT * FROM InventoryActionStatus WHERE InventoryActionID='{00000000-0000-0000-0000-000000000002}'")
            End If
            Dim searcher As New ManagementObjectSearcher(scope, strQuery)
            For Each clObj In searcher.Get
                GetInventoryDate_tmp = clObj.Properties("LastReportDate").Value
            Next

            Return GetInventoryDate_tmp

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function GetGUID(ByVal x As String)
        Try
            Dim GetGUID_tmp As String = ""
            Dim path As New ManagementPath("\\" & pcname & "\root\ccm:CCM_Client")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()

            If x = "ClientId" Then
                Dim searcher As New ManagementObjectSearcher(scope, New ObjectQuery("SELECT * FROM CCM_Client"))
                For Each clObj In searcher.Get
                    GetGUID_tmp = clObj.Properties("ClientId").Value
                Next
            ElseIf x = "ClientIdChangeDate" Then
                Dim searcher As New ManagementObjectSearcher(scope, New ObjectQuery("SELECT * FROM CCM_Client"))
                For Each clObj In searcher.Get
                    GetGUID_tmp = clObj.Properties("ClientIdChangeDate").Value
                Next
            End If
            Return GetGUID_tmp
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function GetMP()
        Try
            Dim GetMP_tmp As String = ""
            Dim path As New ManagementPath("\\" & pcname & "\root\ccm")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()

            Dim searcher As New ManagementObjectSearcher(scope, New ObjectQuery("SELECT * FROM SMS_Authority"))
            For Each clObj In searcher.Get
                GetMP_tmp = clObj.Properties("CurrentManagementPoint").Value
            Next

            Return GetMP_tmp

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function GetClientInfo(ByVal x As String)
        Try
            Dim GetClientInfo_tmp As String = ""
            Dim path As New ManagementPath("\\" & pcname & "\root\ccm:SMS_Client")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()

            If x = "ClientVer" Then
                Dim searcher As New ManagementObjectSearcher(scope, New ObjectQuery("SELECT * FROM SMS_Client"))
                For Each clObj In searcher.Get
                    GetClientInfo_tmp = clObj.Properties("ClientVersion").Value
                Next
            ElseIf x = "SiteCode" Then
                Dim classInstance As New ManagementClass(scope, path, Nothing)
                Dim inParams As ManagementBaseObject = classInstance.GetMethodParameters("GetAssignedSite")
                Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("GetAssignedSite", inParams, Nothing)
                GetClientInfo_tmp = outParams("sSiteCode")
            End If
            Return GetClientInfo_tmp

        Catch ex As Exception
            Return "GetClientInfo: ERR"
        End Try
    End Function

    ' ===========================================================================================
    Function GetOS(ByVal x As String)
        Try

            Dim Scope As New ManagementScope("\\" & pcname & "\root\cimv2", connection)
            Scope.Connect()
            Dim GetOS_tmp As String = ""

            Query = New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
            Dim OS_Search As New ManagementObjectSearcher(Scope, Query)
            For Each objItem In OS_Search.Get
                If x = "OS" Then
                    GetOS_tmp = objItem("Caption")
                    Exit For
                ElseIf x = "SP" Then
                    GetOS_tmp = objItem("CSDVersion")
                    Exit For
                ElseIf x = "ARCH" Then
                    GetOS_tmp = objItem("OSArchitecture")
                    Exit For
                End If
            Next
            Query = Nothing
            Scope = Nothing
            Return GetOS_tmp

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function GetNicConfig(ByVal x As String)
        Try

            Dim Scope As New ManagementScope("\\" & pcname & "\root\cimv2", connection)
            Scope.Connect()
            Dim GetNicConfig_tmp As String = ""

            Query = New ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
            Dim IPConfigSet As New ManagementObjectSearcher(Scope, Query)
            For Each IPConfig In IPConfigSet.Get
                For i = LBound(IPConfig("IPAddress")) To UBound(IPConfig("IPAddress"))
                    If InStr(IPConfig("IPAddress")(i), ":") = 0 Then
                        Dim IpOctet = Split(IPConfig("IPAddress")(i), ".")
                        If IpOctet(0) = "10" Then
                            If x = "IP" Then
                                GetNicConfig_tmp = IPConfig("IPAddress")(i)
                            ElseIf x = "MAC" Then
                                GetNicConfig_tmp = IPConfig("MACAddress")
                            ElseIf x = "Gateway" Then
                                GetNicConfig_tmp = IPConfig("DefaultIPGateway")(i)
                            ElseIf x = "DNSHostName" Then
                                GetNicConfig_tmp = IPConfig("DNSHostName")
                            End If
                        End If
                    End If
                Next
            Next
            Query = Nothing
            Scope = Nothing
            Return GetNicConfig_tmp

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    ' ===========================================================================================

    Function WMIDateStringToDate(dtmInstallDate)
        Try

            If Len(dtmInstallDate) > 0 Then
                Return CDate(Mid(dtmInstallDate, 7, 2) & "/" & Mid(dtmInstallDate, 5, 2) & "/" & Left(dtmInstallDate, 4) _
                                        & " " & Mid(dtmInstallDate, 9, 2) & ":" & Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, 13, 2))
            Else
                Return ""
            End If

        Catch ex As Exception
            Return "WMIDateStringToDate: ERR" & ex.Message
        End Try
        dtmInstallDate = ""
    End Function

    ' ===========================================================================================
    Function DateStringToDate(dDate)
        Try
            If Len(dDate) > 0 Then
                Return CDate(Mid(dDate, 4, 2) & "/" & Left(dDate, 2) & "/" & Mid(dDate, 7, 4) _
                            & " " & Mid(dDate, 12, 2) & ":" & Mid(dDate, 15, 2) & ":" & Mid(dDate, 18, 2))
            Else
                Return ""
            End If

        Catch ex As Exception
            Return "DateStringToDate: ERR"
            'ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function RepairClient()
        Try
            Dim path As New ManagementPath("\\" & pcname & "\root\ccm:SMS_Client")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()

            Dim classInstance As New ManagementClass(scope, path, Nothing)
            Dim inParams As ManagementBaseObject = classInstance.GetMethodParameters("RepairClient")
            classInstance.InvokeMethod("RepairClient", inParams, Nothing)
            Return "RepairClient started... ERR: " & 0

        Catch ex As Exception
            Return "RepairClient: ERR: " & ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function ResetWMI()
        Try

            Dim ResetWMI_tmp As String = ""
            Dim path As New ManagementPath("\\" & pcname & "\root\Cimv2:Win32_Process")
            Dim scope As New ManagementScope(path, connection)
            scope.Connect()

            Dim strCmd As String = ""
            If GetOS("ARCH") = "64-bit" Then
                strCmd = "cmd /c Winmgmt /clearadap &Winmgmt /regserver &Winmgmt /resyncperf &Winmgmt /resetrepository &sc config winmgmt start= disabled &net stop ""sms agent host"" /y &TASKKILL /F /IM CCMExec.exe /T &net stop IAANTMon /y &net stop wscsvc /y &net stop iphlpsvc /y &net stop winmgmt /y &%systemdrive% &cd %windir%\SysWOW64\wbem &rd /S /Q repository &regsvr32 /s %systemroot%\system32\scecli.dll &regsvr32 /s %systemroot%\system32\userenv.dll &mofcomp cimwin32.mof &mofcomp cimwin32.mfl &mofcomp rsop.mof &mofcomp rsop.mfl &(for /f %s in ('dir /b /s *.dll') do regsvr32 /s %s) &(for /f %s in ('dir /b *.mof *.mfl') do mofcomp %s) &wmiprvse /regserver &sc config winmgmt start= auto &net start winmgmt &net start iphlpsvc /y &net start wscsvc /y"
            Else
                strCmd = "cmd /c Winmgmt /clearadap &Winmgmt /regserver &Winmgmt /resyncperf &Winmgmt /resetrepository &sc config winmgmt start= disabled &net stop ""sms agent host"" /y &TASKKILL /F /IM CCMExec.exe /T &net stop IAANTMon /y &net stop wscsvc /y &net stop iphlpsvc /y &net stop winmgmt /y &%systemdrive% &cd %windir%\system32\wbem &rd /S /Q repository &regsvr32 /s %systemroot%\system32\scecli.dll &regsvr32 /s %systemroot%\system32\userenv.dll &mofcomp cimwin32.mof &mofcomp cimwin32.mfl &mofcomp rsop.mof &mofcomp rsop.mfl &(for /f %s in ('dir /b /s *.dll') do regsvr32 /s %s) &(for /f %s in ('dir /b *.mof *.mfl') do mofcomp %s) &wmiprvse /regserver &sc config winmgmt start= auto &net start winmgmt &net start iphlpsvc /y &net start wscsvc /y"
            End If


            Dim classInstance As New ManagementClass(scope, path, Nothing)
            Dim inParams As ManagementBaseObject = classInstance.GetMethodParameters("Create")
            inParams("CommandLine") = strCmd
            Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
            ResetWMI_tmp = outParams("ReturnValue") & " ProcessID: " & outParams("ProcessID")
            Return "Reset WMI started ... (" & ResetWMI_tmp & ")"

        Catch ex As Exception
            Return "RepaireClient: ERR: " & ex.Message
        End Try
    End Function

    ' ===========================================================================================
    Function Reinstall()
        Try

            Dim Reinstall_tmp As String = ""
            Dim NetDirCCMSetup As String = ""
            If InStr(GetNicConfig("IP"), "10.216.") > 0 Then NetDirCCMSetup = "\\U160EKATSMSP01\SMSPKGD$\U0000004"
            If InStr(GetNicConfig("IP"), "10.146.") > 0 Then NetDirCCMSetup = "\\U160EKATSMSP01\SMSPKGD$\U0000004"
            If InStr(GetNicConfig("IP"), "10.136.") > 0 Then NetDirCCMSetup = "\\U0600UFASMSP01\SMSPKGD$\U0000004"
            If InStr(GetNicConfig("IP"), "10.202.") > 0 Then NetDirCCMSetup = "\\U720CHELSMSP01\SMSPKGD$\U0000004"
            If InStr(GetNicConfig("IP"), "10.162.") > 0 Then NetDirCCMSetup = "\\U320KURGSMSP01\SMSPKGD$\U0000004"

            Dim errrr = InstallService(pcname, NetDirCCMSetup & "\ccmsetup.exe", "/runservice SMSSITECODE=U00 FSP=U160EKATSMSP01PATCH=" & NetDirCCMSetup & "\i386\hotfix\KB977384\sccm2007ac-sp2-kb977384-x86-enu.msp", "CCMSetup1")
            Return "Install/Reinstall Started... Err: " & errrr

        Catch ex As Exception
            Return "Reinstall: ERR: " & ex.Message
        End Try
    End Function

    Function Uninstall()
        Try

            Dim Scope As New ManagementScope("\\" & pcname & "\root\cimv2", connection)
            Scope.Connect()
            Dim Uninstall_tmp As String = ""

            Query = New ObjectQuery("SELECT * FROM Win32_Product WHERE Name='Configuration Manager Client'")
            Dim Searcher As New ManagementObjectSearcher(Scope, Query)
            For Each queryObj As ManagementObject In Searcher.Get
                Dim inParams As ManagementBaseObject = queryObj.GetMethodParameters("Uninstall")
                Dim result As ManagementBaseObject = queryObj.InvokeMethod("Uninstall", inParams, Nothing)
                Uninstall_tmp = result.GetPropertyValue("ReturnValue").ToString()
            Next
            Query = Nothing
            Scope = Nothing
            Return "Uninstall completed, Result = " & Uninstall_tmp

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
    ' ===========================================================================================
    Function PingingPC(host As String) As Integer
        Dim doping As New System.Net.NetworkInformation.Ping
        Try
            Dim reply As System.Net.NetworkInformation.PingReply = doping.Send(host)
            Return reply.Status
        Catch ex As Exception
            Return 9999
        End Try
    End Function


    ' ===========================================================================================
    Protected Sub ButtonRepCli_Click(sender As Object, e As EventArgs) Handles ButtonRepCli.Click
        LabelStatus.Text = RepairClient()
    End Sub

    ' ===========================================================================================
    Protected Sub ButtonResetWMI_Click(sender As Object, e As EventArgs) Handles ButtonResetWMI.Click
        LabelStatus.Text = ResetWMI()
    End Sub

    ' ===========================================================================================
    Private Sub ButtonReinstall_Click(sender As Object, e As EventArgs) Handles ButtonReinstall.Click
        LabelStatus.Text = Reinstall()
    End Sub

    ' ===========================================================================================
    Private Sub BtnUninstall_Click(sender As Object, e As EventArgs) Handles BtnUninstall.Click
        LabelStatus.Text = Uninstall()
    End Sub



    ' Установка службы =========================================================================
    Public Function InstallService(sClient As String, sServicepath As String, sParams As String, sServiceName As String)
        Dim yt As String = ""
        Try

            Dim SCM As IntPtr = OpenSCManager(sClient, Nothing, SC_MANAGER_ACCESS.ALL_ACCESS)
            Dim SHandle As IntPtr = CreateService(SCM, sServiceName, sServiceName, SC_MANAGER_ACCESS.ALL_ACCESS, SC_SERVICE_TYPE.INTERACTIVE_PROCESS Or SC_SERVICE_TYPE.WIN32_OWN_PROCESS, SC_START_TYPE.SERVICE_AUTO_START, _
                SC_ERROR_CONTROL.SERVICE_ERROR_NORMAL, (Convert.ToString(sServicepath & Convert.ToString(" ")) & sParams).Trim(), Nothing, New IntPtr(0), Nothing, Nothing, _
                Nothing)

            If SHandle <> IntPtr.Zero Then
                Dim sResult As Boolean = StartService(SHandle, 0, IntPtr.Zero)

                If sResult Then
                    yt = 0
                Else

                    Dim SHandle2 As IntPtr = OpenService(SCM, sServiceName, SC_MANAGER_ACCESS.ALL_ACCESS)
                    DeleteService(SHandle2)
                    CloseServiceHandle(SCM)
                    CloseServiceHandle(SHandle2)
                    yt = "failed to start service"
                End If
            Else
                yt = "failed to install service"
                Dim SHandle2 As IntPtr = OpenService(SCM, sServiceName, SC_MANAGER_ACCESS.ALL_ACCESS)
                DeleteService(SHandle2)
                CloseServiceHandle(SCM)
                CloseServiceHandle(SHandle2)
            End If

        Catch ex As Exception
            yt = ex.Message
        End Try
        Return yt
    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
End Class